#include <rtthread.h>
#include <sys/time.h>
#include <rthw.h>

#include "app_sys_param.h"
#include "app_dbg_util.h"
#include "app_dbg_cmd.h"
#include "app_dbg_mp.h"
#include "app_dbg_sm.h"
#include "app_dbg_drv.h"
#include "app_cse7761_drv.h"
#include "app_ntc_drv.h"
#include "app_security.h"
#include "app_charge_drv.h"
#include "at_device.h"
#include "at_device_esp32.h"
#include "at_device_ec25.h"
#include "at_device_r6.h"
#include "at_sample_client.h"
//#include "drv_clock.h"
#include "app_lcd_drv.h"


#define LOG_TAG                    "dbg.util"
#define LOG_LVL                    LOG_LVL_INFO
#include "ulog.h"


typedef struct
{
    dbg_item_sta_e sta;
    const char *name;

}DBG_ITEM_STA_t;

static DBG_ITEM_STA_t dbg_item_sta[] = 
{
    { DBG_ITEM_STA_NULL,    "null" },
    { DBG_ITEM_STA_READY,   "ready" },
    { DBG_ITEM_STA_WAIT,    "wait"  },
    { DBG_ITEM_STA_PASS,    "pass"  },
    { DBG_ITEM_STA_FAIL,    "fail"  },
    { DBG_ITEM_STA_VALUE,   "value" },    
};

typedef struct
{
    const char *name;
    dbg_item_e item;
    dbg_item_sta_e sta;
    BOOL send_flag;  
    UINT8 time_cnt; 

}DBG_ITEM_TAB_t;

static DBG_ITEM_TAB_t dbg_item_tab[] = 
{
    { "proc_sn",     DBG_ITEM_PROC_SN,      DBG_ITEM_STA_NULL,    FALSE,  0  }, 
    { "flash",       DBG_ITEM_FLASH,        DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "ntc",         DBG_ITEM_NTC,          DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "sw",          DBG_ITEM_SW,           DBG_ITEM_STA_NULL,    FALSE,  0  }, 
    { "key_reset",   DBG_ITEM_KEY_RESET,    DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "key_urgent",  DBG_ITEM_KEY_URGENT,   DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "meter",       DBG_ITEM_METER,        DBG_ITEM_STA_NULL,    FALSE,  3  },  
    { "exter_meter", DBG_ITEM_EXTER_METER,  DBG_ITEM_STA_NULL,    FALSE,  3  },
    { "safe",        DBG_ITEM_SAFE,         DBG_ITEM_STA_NULL,    FALSE,  5  },   
    { "pin_cp",      DBG_ITEM_PIN_CP,       DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "pin_cc",      DBG_ITEM_PIN_CC,       DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "rtc",         DBG_ITEM_RTC,          DBG_ITEM_STA_NULL,    FALSE,  3  },  
    { "at_module",   DBG_ITEM_AT_MODULE,    DBG_ITEM_STA_NULL,    FALSE,  0  },   
    { "sim",         DBG_ITEM_SIM,          DBG_ITEM_STA_NULL,    FALSE,  0  },   
    { "m15118",      DBG_ITEM_M15118,       DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "ethe",        DBG_ITEM_ETHE,         DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "led",         DBG_ITEM_LED,          DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "lcd",         DBG_ITEM_LCD,          DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "nfc",         DBG_ITEM_NFC,          DBG_ITEM_STA_NULL,    FALSE,  0  },  
    { "4g_csq",      DBG_ITEM_4G_CSQ,       DBG_ITEM_STA_NULL,    FALSE,  0  },   
    { "wifi_csq",    DBG_ITEM_WIFI_CSQ,     DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "wifi_mac",    DBG_ITEM_WIFI_MAC,     DBG_ITEM_STA_NULL,    FALSE,  3  },   
    { "ble_mac",     DBG_ITEM_BLE_MAC,      DBG_ITEM_STA_NULL,    FALSE,  3  },
    { "ethe_mac",    DBG_ITEM_ETHE_MAC,     DBG_ITEM_STA_NULL,    FALSE,  3  },     
    { "vh",          DBG_ITEM_VH,           DBG_ITEM_STA_NULL,    FALSE,  0  },
    { "vs",          DBG_ITEM_VS,           DBG_ITEM_STA_NULL,    FALSE,  0  },   
    { "4g_ver",      DBG_ITEM_4G_VER,       DBG_ITEM_STA_NULL,    FALSE,  5  },
    { "wifi_ver",    DBG_ITEM_WIFI_VER,     DBG_ITEM_STA_NULL,    FALSE,  5  },   
    { "ble_ver",     DBG_ITEM_BLE_VER,      DBG_ITEM_STA_NULL,    FALSE,  5  },
    { "4g_imei",     DBG_ITEM_4G_IMEI,      DBG_ITEM_STA_NULL,    FALSE,  5  },   
    { "4g_iccid",    DBG_ITEM_4G_ICCID,     DBG_ITEM_STA_NULL,    FALSE,  5  },
    { "4g_imsi",     DBG_ITEM_4G_IMSI,      DBG_ITEM_STA_NULL,    FALSE,  5  },     
    { "4g_msisdn",   DBG_ITEM_4G_MSISDN,    DBG_ITEM_STA_NULL,    FALSE,  5  },
    { "create_time", DBG_ITEM_CREATE_TIME,  DBG_ITEM_STA_NULL,    FALSE,  0  },   
    { "4g_rssi",     DBG_ITEM_4G_RSSI,      DBG_ITEM_STA_NULL,    FALSE,  5  },
    { "wifi_rssi",   DBG_ITEM_WIFI_RSSI,    DBG_ITEM_STA_NULL,    FALSE,  5  },   

};
#define DBG_ITEM_TAB_SIZE  (ARRAY_SIZE(dbg_item_tab))


static DBG_ITEM_TAB_t dbg_item_tab_default[DBG_ITEM_TAB_SIZE];


//设置测试状态项
void app_dbg_item_set(UINT8 *pdata, UINT16 len)
{
    UINT8 cnt = 0;
    UINT8 tem = 0;
    tem = *pdata;

    for(int i = 0; i < DBG_ITEM_MAX; i++){
        if(GET_BIT(tem, 7-cnt)) {
            dbg_item_tab[i].sta = DBG_ITEM_STA_READY;
        }else{
            dbg_item_tab[i].sta = DBG_ITEM_STA_NULL;
        }
        dbg_item_tab[i].send_flag = FALSE;

        cnt++;
        if(cnt >= 8)
        {
            cnt = 0;
            pdata++; //指向下一字节
            tem = *pdata;
        }
    }

}

//设置标识
void app_dbg_util_item_set(dbg_item_e e_item, dbg_item_sta_e state)
{
    if(FALSE == app_dbg_conn_flag_get()){ //不在产测状态
        return;
    }
    dbg_item_tab[e_item].sta = state;
}


static UINT8 key_urgent[]={0xC7, 0xEB, 0xB0, 0xB4, 0xBC, 0xB1, 0xCD, 0xA3, 0xBC, 0xFC, 0x2E, 0x2E, 0x2E};  //"请按急停键..."
static UINT8 key_reset[]={0xC7, 0xEB, 0xB0, 0xB4, 0xB8, 0xB4, 0xCE, 0xBB, 0xBC, 0xFC, 0x2E, 0x2E, 0x2E};   //"请按复位键..."
static UINT8 swipe_card[]={0xC7, 0xEB, 0xCB, 0xA2, 0xBF, 0xA8, 0x2E, 0x2E, 0x2E};   //"请刷卡..."

static void app_dbg_util_item_send(dbg_item_e e_item, char *s, UINT16 len)
{
    if(e_item >= DBG_ITEM_MAX){
        return;
    }

    UINT8 strtem[100]={0};
    UINT16 ret_len = 0;
    dbg_item_sta_e state = dbg_item_tab[e_item].sta;
    
    if(s != NULL){
        ret_len = len;
        rt_memcpy(strtem, s, len);     
    }
    
    switch(e_item){
              
        case DBG_ITEM_KEY_URGENT:                   //急停按钮
            if(state == DBG_ITEM_STA_WAIT){
                ret_len = sizeof(key_urgent);
                rt_memset(strtem, 0, sizeof(strtem));
                rt_memcpy(strtem, key_urgent, ret_len);                  
            }else{
                ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[e_item].name, dbg_item_sta[dbg_item_tab[e_item].sta].name);  
            }

            break;

        case DBG_ITEM_KEY_RESET:                    //复位按键
            if(state == DBG_ITEM_STA_WAIT){
                ret_len = sizeof(key_reset);
                rt_memset(strtem, 0, sizeof(strtem));
                rt_memcpy(strtem, key_reset, ret_len);           
            }else{
                ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[e_item].name, dbg_item_sta[dbg_item_tab[e_item].sta].name);  
            }
            break ;    

        case DBG_ITEM_NFC:                          //刷卡板
            if(state == DBG_ITEM_STA_WAIT){
                ret_len = sizeof(swipe_card);
                rt_memset(strtem, 0, sizeof(strtem));
                rt_memcpy(strtem, swipe_card, ret_len);           
            }else{
                ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[e_item].name, dbg_item_sta[dbg_item_tab[e_item].sta].name);  
            }
            break ; 

        default:
            break ;    
    }

    app_dbg_drv_pkg_send(DBG_CMD_SIG_INFO, strtem, ret_len);
    if( (DBG_ITEM_STA_PASS==state)||(DBG_ITEM_STA_FAIL==state)||(DBG_ITEM_STA_VALUE==state) ){
        dbg_item_tab[e_item].send_flag = TRUE;   //DBG_ITEM_STA_WAIT 的需等待最后通过
    } 
    
}


//产测项处理
void app_dbg_util_item_proc()
{
    UINT16 i;
    UINT16 u16dat=0, uflash=0x1234;
    UINT32 ret=0;
    BOOL finish = TRUE;
    char strtem[100]={0};
    UINT8 dat=0;
    int dat1,dat2,dat3;

    char strtem2[100]={0};
    UINT16 ret_len = 0;

    esp32_device_info esp32={0};
    ec25_device_info ec25={0};
	r6_device_info r6 = {0};

    if(!app_dbg_conn_flag_get()){ //产测未连接
        return;
    }
    if(!app_dbg_drv_item_get()){  //测试项未设置
        return;
    }

    for(i=0; i<DBG_ITEM_MAX; i++){
        if(dbg_item_tab[i].time_cnt>0){  //延时计数
            dbg_item_tab[i].time_cnt--;
        }
    }

    for(i=0; i<DBG_ITEM_MAX; i++){
        if(TRUE == dbg_item_tab[i].send_flag){      //测试发送完成项
            continue;
        }
        if( DBG_ITEM_STA_NULL == dbg_item_tab[i].sta ) {   //不做测试项
            dbg_item_tab[i].send_flag = TRUE; //默认完成
            continue;
        }      
        if( (dbg_item_tab[i].sta==DBG_ITEM_STA_PASS) || (dbg_item_tab[i].sta==DBG_ITEM_STA_FAIL)){  //测试通过项
            app_dbg_util_item_send(dbg_item_tab[i].item, NULL, 0);
            continue;
        }
        if(dbg_item_tab[i].sta==DBG_ITEM_STA_WAIT){ //等待状态  当前测试项还未完成
            break;
        }  
        
        rt_memset(strtem, 0, sizeof(strtem));
        rt_memset(strtem2, 0, sizeof(strtem2));
        if( (dbg_item_tab[i].sta==DBG_ITEM_STA_READY) && (dbg_item_tab[i].time_cnt==0) ){
            switch(dbg_item_tab[i].item){

                case DBG_ITEM_FLASH:                        //FLASH
                    app_sys_param_set(APP_SYS_PARAM_DBG_TEST, SYS_PARAM_TYPE_HEX, &uflash);
                    app_sys_param_get(APP_SYS_PARAM_DBG_TEST, &u16dat, sizeof(u16dat));
                    if(u16dat==uflash){
                        app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    }else{
                        app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_FAIL);
                    }
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, dbg_item_sta[dbg_item_tab[i].sta].name); 
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_METER:                        //板载计量芯片
//                    if(app_meter_fault_get()){ //电表故障
//                        app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_FAIL);
//                    }else{
//                        app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
//                    }
//                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, dbg_item_sta[dbg_item_tab[i].sta].name); 
                    break;

                case DBG_ITEM_EXTER_METER:                    //外接计量485接口
                    break;

                case DBG_ITEM_NTC:                          //NTC电阻             
#if defined(CHARGER_80A)
					app_ntc_get_temp(&dat1, NTC_ADC_CH);
                    app_ntc_get_temp(&dat2, RELAY_L_NTC_ADC_CH);
                    app_ntc_get_temp(&dat3, RELAY_N_NTC_ADC_CH);
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%d/%d/%d", dbg_item_tab[i].name, dat1, dat2, dat3);
#elif defined(CHARGER_48A)
					app_ntc_get_temp(&dat1);
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%d", dbg_item_tab[i].name, dat1); 
#endif
                     
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_VALUE);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_SAFE:                         //安全检测
                    ret = 0;
                    app_security_get_fault(&ret);
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%u", dbg_item_tab[i].name,  (unsigned int)ret); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_VALUE);                    
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                break;

                case DBG_ITEM_SW:                           //编码旋钮
                    dat = app_charge_current_code_get();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%u", dbg_item_tab[i].name,  (unsigned int)dat); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_VALUE);                    
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_KEY_RESET:                    //复位按键
                case DBG_ITEM_KEY_URGENT:                   //急停按钮
                case DBG_ITEM_NFC:                          //刷卡板
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_WAIT);
                    app_dbg_util_item_send(dbg_item_tab[i].item, NULL, 0);
                    i = 0; //DBG_ITEM_STA_WAIT 需从头开始扫描
                    break;
                
                case DBG_ITEM_PIN_CP:                       //CP脚
                case DBG_ITEM_PIN_CC:                       //CC脚 
                    break;

                case DBG_ITEM_RTC:                          //RTC
                    // if( (0==clock_state())||(0==clock_xtl_state())){
                    //     app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_FAIL);
                    //     app_dbg_util_item_send(dbg_item_tab[i].item, NULL);                        
                    // }else{
                    //     app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    //     app_dbg_util_item_send(dbg_item_tab[i].item, NULL);
                    // }
                    break;

                case DBG_ITEM_AT_MODULE:                    //AT模组
                    // if(RT_EOK == app_get_at_order_state()){
                    //     app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    //     app_dbg_util_item_send(dbg_item_tab[i].item, NULL);
                    // }else if(RT_ERROR == app_get_at_order_state()){
                    //     app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_FAIL);
                    //     app_dbg_util_item_send(dbg_item_tab[i].item, NULL);
                    // }else{ //RT_EBUSY 

                    // }
                    break;
                    
                case DBG_ITEM_SIM:                          //SIM卡
                case DBG_ITEM_M15118:                       //15118模块
                case DBG_ITEM_ETHE:                         //以太网
                case DBG_ITEM_LED:                          //LED
                    break;

                case DBG_ITEM_LCD:                          //LCD
                    dat = LCD_RX_VER_BYTE;
                    app_lcd_ver_clr();
                    drv_lcd_uart_send(LCD_CMD_READ, DRV_LCD_CMD_PAGE_VERSION, &dat, 1);  //读取版本
                    rt_thread_mdelay(250);
                    app_lcd_ver_get((char *)strtem2, sizeof(strtem2));                  
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, strtem2); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);                 
                    break;


                
                case DBG_ITEM_4G_CSQ:                       //4g信号强度
                case DBG_ITEM_WIFI_CSQ:                     //wifi信号强度
                    break;
        
                case DBG_ITEM_PROC_SN:                      //生产SN号
                    app_sys_param_get(APP_SYS_PARAM_PROC_SN, strtem2, sizeof(strtem2));
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, strtem2); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_VH:                           //硬件版本号
                    app_sys_param_get(APP_SYS_PARAM_VH, strtem2, sizeof(strtem2));
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, strtem2); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_VS:                           //软件版本号   
                    app_sys_param_get(APP_SYS_PARAM_VS, strtem2, sizeof(strtem2));
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, strtem2); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;

                case DBG_ITEM_WIFI_MAC:                     //WIFI MAC       
#ifdef AT_DEVICE_USING_ESP32                    
                    esp32 = esp32_device_get_info();
#endif
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, esp32.wifi_mac); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;                 

                case DBG_ITEM_BLE_MAC:                      //BLE  MAC          
        //            app_get_esp_ble_mac( (char *)strtem2);
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, strtem2); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;    

                case DBG_ITEM_ETHE_MAC:                     //以太网模组MAC

                    break;

                case DBG_ITEM_WIFI_VER:                     //WIFI 版本     
#ifdef AT_DEVICE_USING_ESP32                    
                    esp32 = esp32_device_get_info();
#endif
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s/%s", dbg_item_tab[i].name, esp32.sdk_version,esp32.bin_version); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;                          

                case DBG_ITEM_4G_VER:                       //4G 版本      
#if defined(A5158_EDITION)	
					rt_memset(&r6, 0, sizeof(r6));
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.mode_version); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break; 
		
#elif defined(STANDARD_EDITION)	
					rt_memset(&ec25, 0, sizeof(ec25));
                    ec25 = ec25_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, ec25.mode_version); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;               
	
#endif				

                case DBG_ITEM_4G_IMEI: 
#if defined(A5158_EDITION)	
                    rt_memset(&r6, 0, sizeof(r6));          
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.imei); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;
		
#elif defined(STANDARD_EDITION)	              
                    rt_memset(&ec25, 0, sizeof(ec25));          
                    ec25 = ec25_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, ec25.imei); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break; 	
#endif

                case DBG_ITEM_4G_ICCID:
#if defined(A5158_EDITION)	
                    rt_memset(&r6, 0, sizeof(r6));          
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.iccid); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break; 
		
#elif defined(STANDARD_EDITION)	              
                    rt_memset(&ec25, 0, sizeof(ec25));          
                    ec25 = ec25_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, ec25.iccid); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break; 	
#endif

                case DBG_ITEM_4G_IMSI:
#if defined(A5158_EDITION)	
                    rt_memset(&r6, 0, sizeof(r6));          
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.imsi); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
					break;  
		
#elif defined(STANDARD_EDITION)	              
                    rt_memset(&ec25, 0, sizeof(ec25));          
                    ec25 = ec25_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, ec25.imsi); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
					break;  	
#endif

                case DBG_ITEM_4G_MSISDN: 
#ifdef	A5158_EDITION
                    rt_memset(&r6, 0, sizeof(r6));          
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.msisdn); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
					break;
#endif				
                case DBG_ITEM_CREATE_TIME:       

                    break;   

                case DBG_ITEM_4G_RSSI:
#ifdef	A5158_EDITION
                    rt_memset(&r6, 0, sizeof(r6));          
                    r6 = r6_device_get_info();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%s", dbg_item_tab[i].name, r6.rssi); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_PASS);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
					break;
#endif					
                case DBG_ITEM_WIFI_RSSI:                    
                    dat1 = get_at_client_rssi();
                    ret_len = snprintf((char *)strtem, sizeof(strtem), "%s=%d", dbg_item_tab[i].name, dat1); 
                    app_dbg_util_item_set(dbg_item_tab[i].item, DBG_ITEM_STA_VALUE);
                    app_dbg_util_item_send(dbg_item_tab[i].item, strtem, ret_len);
                    break;      
                

                default:
                    break ;           
            }  
        }
    }   

    for(i=0; i<DBG_ITEM_MAX; i++){  //判断测试项是否完成
        if( dbg_item_tab[i].send_flag==FALSE ){
            return;
        }
    }
    app_dbg_disconn_msg_post(DBG_VAL_DONE);   //测试完成，连接关闭
}

/* 定时器  超时函数 */
static void dbg_timer_bck(void *parameter)
{
    if(FALSE == app_dbg_conn_flag_get()){ //产测未连接
        return;
    }
  	app_dbg_timer_msg_post();
}

//初始化
int32_t app_dbg_util_init()
{
    UINT8 i;

    if(DBG_ITEM_TAB_SIZE != DBG_ITEM_MAX){
        log_e("app_dbg_util_init(num err)");
        return -1;
    }

	for (i = DBG_ITEM_START; i < DBG_ITEM_MAX; i++) {
		if(i != dbg_item_tab[i].item) {
            log_e("app_dbg_util_init(enum err)");
			return -1;
		}
	}    

	if (!app_dbg_drv_init()){
		log_e("app_dbg_drv_init(err)");	
		return -1;
	}    
    app_dbg_sm_init();

    rt_memcpy(dbg_item_tab_default, dbg_item_tab, sizeof(dbg_item_tab));  //保存默认配置
    return 0;
}
INIT_APP_EXPORT(app_dbg_util_init);
